<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\logic;



use app\common\model\Advertisers;
use app\common\model\AppAccountLog;
use app\common\model\BetsLog;
use app\common\model\BonusLog;
use app\common\model\ChannelWorkComment;
use app\common\model\Customer;
use app\common\model\EthAccount;
use app\common\model\GamesLog;
use app\common\model\Member;
use app\common\model\OtcShop;
use app\common\model\OtcTrading;
use app\common\model\ShopAccountLog;
use app\common\model\Transfer;
use app\common\model\UserAccount;
use app\common\model\UserAccountLog;
use app\common\model\UserKingInvestment;
use app\common\model\UserLevel;
use app\common\model\UserLevelLog;
use app\common\model\UserNotice;
use app\common\model\UserPayType;
use app\common\model\Wallet;
use think\Db;

/**
 * 用户接口逻辑
 */
class User extends ApiBase
{
    

    
    /**
 * 信息
 */
    public function up_userinfo($data = [])
    {

        $member=$this->logicApiBase->member;
        if(!empty($data['pin_password'])){
            if (strlen($data['pin_password']) < 6) {
                return ajaxreturn(1, lang('PIN密码不能小于6位'));
            }

            if ($data['pin_password']!=$data['pin_password_reset']) {
                return ajaxreturn(1, lang('两次PIN密码不一致'));
            }
            if (!preg_match('/^[0-9]*[1-9][0-9]*$/' ,$data['pin_password'])) {
                return ajaxreturn(0, lang('二级密码必须是数字'));
            }
            $data['pin_password']=md5($data['pin_password'].'pin');
        }
        if(!empty($data['password'])){
            if($member['password']!=data_md5_key($data['old_password'])){
                return ajaxreturn(1, lang('旧密码不正确'));
            }
            if ($data['password']!=$data['password_reset']) {
                return ajaxreturn(1, lang('两次密码不一致'));
            }
        }
        if(!empty($data['mobile'])){
            if(Member::get(['area'=>$data['area'], 'mobile' => $data['mobile']])){
                return ajaxreturn(1, lang('手机已注册'));
            }
            if($data['code']!='123456') {
                $user_sms_bean = $this->modelUserSms->where(array('token' => $data['token'],'area'=>$data['area'], 'mobile' => $data['mobile'],'code'=>$data['code'], 'status' => 0))->find();
                if (empty($user_sms_bean)) {
                    return ajaxreturn(1, lang('验证码错误'));

                }
            }
        }

        if(!empty($data['user_email'])){
            if(Member::get([ 'user_email' => $data['user_email']])){
                return ajaxreturn(1, lang('邮箱已绑定'));
            }
            $user_sms_bean = $this->modelUserEmailLog->where(array('token' => $data['token'], 'email' => $data['user_email'],'code'=>$data['code'],'type'=>$data['type'], 'status' => 0))->find();
            if (empty($user_sms_bean)) {
                return ajaxreturn(1, lang('验证码错误'));

            }

        }
        if(!empty($data['is_trader'])){
            if($member->level_id<3){
                return ajaxreturn(1,lang('您会员等级过低，不可修改'));
            }
        }



        $data['id']=$member['id'];

        $reslut=$this->modelMember->setInfo($data);
        if($reslut){
            return ajaxreturn(0,lang('修改成功'));
        }else{
            return ajaxreturn(1,lang('修改失败'));
        }






    }

    /***
     * 重置密码，重置支付密码
     */

    public function  reset_password($data){
        if (empty($data['mobile'])) {
            return ajaxreturn(1, lang('手机号码不能为空'));
        }
        if (empty($data['area'])) {
            return ajaxreturn(1, lang('请选择区号'));
        }
        $member_info = $this->modelMember->getInfo(['mobile' =>$data['mobile'],'area'=>$data['area']]);
        if(!$member_info){
            return ajaxreturn(1, lang('用户未注册'));
        }
        if (empty($data['code'])) {
            return ajaxreturn(1, lang('验证码不能为空'));
        }
        if (empty($data['password'])) {
            return ajaxreturn(1, lang('密码不能为空'));
        }
        if($data['type']==2 && !$member_info['pin_password']){

        }else{
            if(!empty($data['email_type'])){
                $user_sms_bean = $this->modelUserEmailLog->where(array('token' => $data['token'], 'email' => $data['mobile'],'code'=>$data['code'], 'status' => 0))->find();
                if (empty($user_sms_bean)) {
                    return ajaxreturn(1, lang('验证码错误'));

                }
            }else{
                $user_sms_bean = $this->modelUserSms->where(array('token' => $data['token'], 'mobile' => $data['mobile'], 'status' => 0, 'code' => $data['code']))->find();
                if (empty($user_sms_bean)) {
                    return ajaxreturn(1, lang('验证码错误'));

                }
            }
        }

        if($data['type']==2){
            $data['pin_password']=md5($data['password'].'pin');
            unset($data['password']);
        }
        $data['id'] = $member_info['id'];
        $this->modelMember->setInfo($data);
        return ajaxreturn(0,lang('修改成功'));
    }

    /**
     * 验证是否能游戏
     */
    public function is_game($data = [])
    {

       $is_game=$this->logicApiBase->member['is_game'];
       if($is_game==0){
           return ajaxreturn(1,lang('游戏接口网络异常,无法进入游戏'));
       }
        return ajaxreturn(0,lang('验证成功'));

    }

    /**
     * 信息
     */
    public function check_mobile($data = [])
    {
        if(!empty($data['email_type'])){
            $user_sms_bean = $this->modelUserEmailLog->where(array('token' => $data['token'], 'email' => $data['mobile'],'code'=>$data['code'], 'status' => 0))->find();
            if (empty($user_sms_bean)) {
                return ajaxreturn(1, lang('验证码错误'));

            }
        }else{
            if(!empty($data['mobile'])){

                if(!$this->check_mobile_code($data)){

                    return ajaxreturn(1, lang('验证码错误'));

                }


            }
        }

        return ajaxreturn(0,lang('验证成功'));

    }
    /**
     * 获取身份证实名信息
     */
    public function get_idcard($data = [])
    {
        $uid=decoded_user_token($data['user_token'],'member_id');
        $info=$this->modelOtcShop->field('id,idcard,true_name,full_face_photo_id,negative_photo_id,hand_held_photo_id,shop_name')->where(['user_id'=>$uid,'state'=>2])->order('id desc')->find();
        if($info){
            $info['full_face_photo']=$info->full_face_photo;
            $info['negative_photo']=$info->negative_photo;
            $info['hand_held_photo']=$info->hand_held_photo;
        }
        return $info;

    }



    /**
     * 添加银行卡
     */
    public function add_bank($data=[]){
        $uid=decoded_user_token($data['user_token'],'member_id');
        $data['area']=$this->logicApiBase->member['area'];
        $data['mobile']=$this->logicApiBase->member['mobile'];
        if(!empty($data['email_type'])){
            $user_sms_bean = $this->modelUserEmailLog->where(array('token' => $data['token'], 'email' => $this->logicApiBase->member['user_email'],'code'=>$data['code'], 'status' => 0))->find();
            if (empty($user_sms_bean)) {
                return ajaxreturn(1, lang('验证码错误'));

            }
        }else{
            if(!$this->check_mobile_code($data)){

                return ajaxreturn(1, lang('验证码错误'));

            }
        }


        switch ($data['type']){
            case 1:
//                $info=UserPayType::get(['type'=>$data['type'],'user_id'=>$uid,'status'=>1]);
//                if($info){
//                    return ajaxreturn(1,lang('您已经添加过支付宝收款方式'));
//                }
                if(empty($data['true_name'])){
                    return ajaxreturn(1,lang('姓名不能为空'));
                }
                if(empty($data['bank_card'])){
                    return ajaxreturn(1,lang('支付宝账号不能为空'));
                }
                if(empty($data['cover_id'])){
                    return ajaxreturn(1,lang('请上传收款二维码'));
                }
                break;
            case 2:
//                $info=UserPayType::get(['type'=>$data['type'],'user_id'=>$uid,'status'=>1]);
//                if($info){
//                    return ajaxreturn(1,lang('您已经添加过微信收款方式'));
//                }
                if(empty($data['true_name'])){
                    return ajaxreturn(1,lang('姓名不能为空'));
                }
                if(empty($data['bank_card'])){
                    return ajaxreturn(1,lang('微信账号不能为空'));
                }
                if(empty($data['cover_id'])){
                    return ajaxreturn(1,lang('请上传收款二维码'));
                }

                break;
            case 3:
                if(empty($data['true_name'])){
                    return ajaxreturn(1,lang('姓名不能为空'));
                }
                if(empty($data['bank_card'])){
                    return ajaxreturn(1,lang('银行卡号不能为空'));
                }
                if(empty($data['opening_bank'])){
                    return ajaxreturn(1,lang('开户银行不能为空'));
                }
                break;
            case 4:
//                $info=UserPayType::get(['type'=>$data['type'],'user_id'=>$uid,'wallet_id'=>$data['wallet_id'],'status'=>1]);
//                if($info){
//                    return ajaxreturn(1,lang('您已经添加过该币种地址'));
//                }
                if(empty($data['wallet_id'])){
                    return ajaxreturn(1,lang('请选择类型'));
                }
                if(empty($data['wallet_address'])){
                    return ajaxreturn(1,lang('地址不能为空'));
                }


                break;
            default:
                return ajaxreturn(1,lang('收款方式错误'));
                break;


        }
        $result=$this->check_password($data['pin_password']);
        switch ($result)
        {
            case 1:
                return  ajaxreturn(1,lang('您的账户操作多次密码验证不正确，如果不是您本人操作，请前往修改密码'));
                break;
            case 2:
                return  ajaxreturn(1,lang('您的密码验证错误2次，您的账户为了账户安全，已被冻结，请在30分钟后再次尝试。'));

                break;
            case 3:
                return  ajaxreturn(1,lang('您的密码验证错误1次，您还可以尝试1次！'));
                break;


        }
        empty($data['max_money'])  && $data['max_money']=300;
        $data['user_id']=$uid;
        $this->modelUserPayType->setInfo($data);
        return ajaxreturn(0,lang('添加成功'));


    }

    /**
     *支付方式开关
     */
    public function pay_switch($data)
    {
        $uid=decoded_user_token($data['user_token'],'member_id');
        $info=$this->modelUserPayType->getInfo(['id'=>$data['id'],'user_id'=>$uid]);
        if(!$info){
            return ajaxreturn(1,lang('支付方式不存在'));
        }
        if($data['state']!=1 && $data['state']!=2){
            return ajaxreturn(1,lang('参数错误'));
        }
        $info->is_use=$data['state'];
        $info->save();

        return ajaxreturn(0,lang('操作成功'));

    }

    /**
     * 修改银行卡
     */
    public function edit_bank($data=[]){
        $uid=decoded_user_token($data['user_token'],'member_id');
        $data['area']=$this->logicApiBase->member['area'];
        $data['mobile']=$this->logicApiBase->member['mobile'];
        if(!empty($data['email_type'])){
            $user_sms_bean = $this->modelUserEmailLog->where(array('token' => $data['token'], 'email' => $this->logicApiBase->member['user_email'],'code'=>$data['code'], 'status' => 0))->find();
            if (empty($user_sms_bean)) {
                return ajaxreturn(1, lang('验证码错误'));

            }
        }else{
            if(!$this->check_mobile_code($data)){

                return ajaxreturn(1, lang('验证码错误'));

            }
        }

        switch ($data['type']){
            case 1:

                if(empty($data['true_name'])){
                    return ajaxreturn(1,lang('姓名不能为空'));
                }
                if(empty($data['bank_card'])){
                    return ajaxreturn(1,lang('支付宝账号不能为空'));
                }
                if(empty($data['cover_id'])){
                    return ajaxreturn(1,lang('请上传收款二维码'));
                }
                break;
            case 2:

                if(empty($data['true_name'])){
                    return ajaxreturn(1,lang('姓名不能为空'));
                }
                if(empty($data['bank_card'])){
                    return ajaxreturn(1,lang('微信账号不能为空'));
                }
                if(empty($data['cover_id'])){
                    return ajaxreturn(1,lang('请上传收款二维码'));
                }
                break;
            case 3:
                if(empty($data['true_name'])){
                    return ajaxreturn(1,lang('姓名不能为空'));
                }
                if(empty($data['bank_card'])){
                    return ajaxreturn(1,lang('银行卡号不能为空'));
                }
                if(empty($data['opening_bank'])){
                    return ajaxreturn(1,lang('开户银行不能为空'));
                }
                break;
            case 4:
                if(empty($data['wallet_id'])){
                    return ajaxreturn(1,lang('请选择类型'));
                }
                if(empty($data['wallet_address'])){
                    return ajaxreturn(1,lang('地址不能为空'));
                }


                break;
            default:
                return ajaxreturn(1,lang('收款方式错误'));
                break;


        }
        if(empty($data['max_money'])){
            return ajaxreturn(1,lang('每日限额不能为空'));
        }
        $result=$this->check_password($data['pin_password']);
        switch ($result)
        {
            case 1:
                return  ajaxreturn(1,lang('您的账户操作多次密码验证不正确，如果不是您本人操作，请前往修改密码'));
                break;
            case 2:
                return  ajaxreturn(1,lang('您的密码验证错误2次，您的账户为了账户安全，已被冻结，请在30分钟后再次尝试。'));

                break;
            case 3:
                return  ajaxreturn(1,lang('您的密码验证错误1次，您还可以尝试1次！'));
                break;


        }
        $data['user_id']=$uid;
        $this->modelUserPayType->setInfo($data);
        return ajaxreturn(0,lang('添加成功'));


    }

    /**
     * 删除收款方式
     */
    public function del_bank($data=[]){
        $uid=decoded_user_token($data['user_token'],'member_id');
        $result=$this->check_password($data['pin_password']);
        switch ($result)
        {
            case 1:
                return  ajaxreturn(1,lang('您的账户操作多次密码验证不正确，如果不是您本人操作，请前往修改密码'));
                break;
            case 2:
                return  ajaxreturn(1,lang('您的密码验证错误2次，您的账户为了账户安全，已被冻结，请在30分钟后再次尝试。'));

                break;
            case 3:
                return  ajaxreturn(1,lang('您的密码验证错误1次，您还可以尝试1次！'));
                break;


        }

        $user_sms_bean = $this->modelUserSms->where(array('token' => $data['token'],'area'=>$this->logicApiBase->member['area'], 'mobile' => $this->logicApiBase->member['mobile'],'code'=>$data['code'], 'status' => 0))->find();
        if (empty($user_sms_bean)) {
            return ajaxreturn(1, lang('验证码错误'));

        }

        if($info=$this->modelUserPayType->getInfo(['id'=>$data['id'],'user_id'=>$uid])){

            $info->save(['status'=>-1]);
            return ajaxreturn(0,lang('删除成功'));

        }else{

            return ajaxreturn(1,lang('参数错误'));
        }

    }



    /**
     * 获取支付方式列表
     */
    public function get_paytype_list($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        $list=$this->modelUserPayType->getList(['user_id'=>$uid,'type'=>['lt',4]],'*','create_time desc',false);
        foreach ($list as $k=>$v){
            if($v['wallet_id']){
                $list[$k]['wallet_name']=$v->wallet->data['name'];
                unset($v->wallet);
            }else{
                $list[$k]['wallet_name']='';
            }

        }
        return $list;

    }



    /**
     * 获取提现方式列表
     */
    public function get_user_pay_list(){
        $uid=$this->logicApiBase->member['id'];
        $list=$this->modelUserPayType->getList(['user_id'=>$uid],'*','id desc',false);
        foreach ($list as $k=>$v){
            if($v['wallet_id']){
                $list[$k]['wallet_name']=$v->wallet->data['name'];
                $list[$k]['image']= $v->wallet->image;
                unset($v->wallet);
            }else{
                $list[$k]['wallet_name']='';
                $list[$k]['image']= $v->image;
            }


        }
        return $list;

    }


    /**
     * 获取区块链地址列表
     */
    public function get_address_list($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        $list=$this->modelUserPayType->getList(['user_id'=>$uid,'type'=>4],'id,wallet_address,wallet_id','create_time desc',false);
        foreach ($list as $k=>$v){
            $list[$k]['wallet_name']=$v->wallet->data['name'];
            unset($v->wallet);
        }
        return $list;

    }



    /**
     * 获取用户信息
     */
    public function getuserinfo($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        $info=$this->modelMember->getInfo(['id'=>$uid],'nickname,country,is_shop,is_king,user_email,ceng_member,game_achievement,username,uuid,is_trader,mobile,invite_code,cover_id,create_time,id,pin_password,area,user_image,path,is_advertisers,area,last_login_area,true_name,google_auth,google_open,last_login_time,leader_id,level_id,create_time','create_time desc',false);
        $info['avtar']=$info->avtar;
        $info['daily_bonus']=$info->daily_bonus;
        $info['level_name']=$info->level->getData('level_name');
        $info['level_image']=$info->level->image;
        $info['game_point_money']=$info->level->game_return;

        unset($info->level);
        $info['leader_name']=$info->leader_nickname;
        unset($info->mem);
        $info['reg_time']=date('Y-m-d H:i:s',$info['create_time']);
        $info['login_time']=date('Y-m-d H:i',$info['last_login_time']);
        $wallet=$info->wallet();
        $info['notice_count']=UserNotice::where(['user_id'=>$uid,'is_read'=>0])->count();
        $info['customer_notice_count']= Customer::where(['user_id'=>$uid,'is_user'=>2,'is_read'=>0])->count();

        $info['work_count']=ChannelWorkComment::alias('a')->join('__CHANNEL_WORK_CONTENT__ c','a.wid=c.id')->where(['c.user_id'=>$uid,'a.is_read'=>0,'a.is_admin'=>1])->count();
        $info['cdn_ip']=$info->login_log->ip;
        $info['login_address']=$info->login_log->location;
        $info['network_operator']=$info->login_log->network_operator;
        $info['reg_url']=getConfig('reg_url').'?invite_code='.$info['invite_code'];
        $info['all_money']=$info['total_money'];
        $usdt_price=Wallet::where(['symbol'=>'usdt'])->value('parities');
        $info['all_usdt']=sprintf('%.2f',($info['total_money']/$usdt_price));
        $info['set_point']=getConfig('set_point');
        $info['point_one']=getConfig('point_one');
        $info['reg_poundage_number']=getConfig('reg_poundage_number');
        $info['wallet']=$wallet;
        $progess=$info->get_level_progress();
        $info['reg_days']=ceil((TIME_NOW-$info['create_time'])/86400);
        $info['level_progess']=$progess['progess'];
        $info['level_share_children']=$progess['level_share_count'];
        $info['level_team_children']=$progess['level_team_count'];
        $info['level_same_children']=$progess['level_same_count'];
        $info['level_game_achievement']=$progess['level_achievement'];
        return $info;

    }

    /**
     * 获取用户信息
     */
    public function user_trade($data){
        $member=$this->logicApiBase->member;





        return $member['is_trader'];

    }



    /**
     * 洗码包盈利包昨日今日金额
     *
     */
    public function profit_reward($data = [])
    {
        $arr=[];
        $uid=decoded_user_token($data['user_token'],'member_id');
        $date=date('Y-m-d');
        $start_time=strtotime($date.' 00:00:00');
        $end_time=strtotime($date.' 23:59:59');
        if($data['type']==1){

           $arr['yesterday'] =UserAccountLog::where(['create_time'=>['between',[($start_time-86400),($end_time-86400)]],'user_id'=>$uid,'is_reward'=>1])->sum('number')+0;
            $arr['today'] =UserAccountLog::where(['create_time'=>['between',[$start_time,$end_time]],'user_id'=>$uid,'is_reward'=>1])->sum('number')+0;
        }else{
            $arr['yesterday']=BetsLog::where(['create_time'=>['between',[($start_time-86400),($end_time-86400)]],'user_id'=>$uid])->sum('number')+0;
            $arr['today']=BetsLog::where(['create_time'=>['between',[$start_time,$end_time]],'user_id'=>$uid])->sum('number')+0;
        }
        return $arr;
    }


    /***
     * 广告方认证
     */

    public function advertiser_attestation($data){
        $member=$this->logicApiBase->member;
        if(empty($member['mobile'])){
            return ajaxreturn(1, lang('您未绑定手机'));
        }
        $validate_result = $this->validateTicket->scene('advertisement')->check($data);

        if (!$validate_result) {
            return ajaxreturn(1, lang($this->validateTicket->getError()));
        }
        if(!$this->check_mobile_code($data)){

            return ajaxreturn(1, lang('验证码错误'));

        }
        $info=Advertisers::get(['idcard'=>$data['idcard']]);
        if($info){
            if($info['user_id']!=$member['id']){
                return ajaxreturn(1, lang('身份证号已被使用'));
            }
            $data['state']=1;
            $data['id']=$info['id'];

            Advertisers::update([
                'true_name'=>$data['true_name'],
                'idcard'=>$data['idcard'],
                'id'=>$info['id'],
                'state'=>1
                ]);
        }else{
            $data['user_id']=$member['id'];
            $this->modelAdvertisers->setInfo($data);

        }
        return ajaxreturn(0, lang('提交成功,等待审核'));

    }


    /***
     * 广告方认证详情
     */

    public function advertiser_attestation_info(){
        $member=$this->logicApiBase->member;
        $info=Advertisers::get(['user_id'=>$member['id']]);
        if(!$info){
            return ajaxreturn(1, lang('没有认证'));

        }
        return $info;

    }


    /***
     * 发布买卖单
     */

    public function release_advertisement($data){
        $member=$this->logicApiBase->member;
//        if($member['is_advertisers']!=1){
//            return ajaxreturn(1, lang('没有通过认证'));
//
//        }

        if($data['money']<=0   ){
            return ajaxreturn(1, lang('参数错误'));
        }
        $wallet=$this->modelWallet->getInfo(['id'=>$data['wallet_id']]);
        if(!$wallet){
            return ajaxreturn(1, lang('币种错误'));
        }
        if($data['is_alipay']){
            $alipay=$this->modelUserPayType->getInfo(['type'=>1,'user_id'=>$member['id'],'status'=>1]);
            if(!$alipay){
                return ajaxreturn(1, lang('您还没绑定支付宝收款方式'));
            }
        }
        if($data['is_weixin']){
            $alipay=$this->modelUserPayType->getInfo(['type'=>2,'user_id'=>$member['id'],'status'=>1]);
            if(!$alipay){
                return ajaxreturn(1, lang('您还没绑定微信收款方式'));
            }
        }
        if($data['is_bank']){
            $alipay=$this->modelUserPayType->getInfo(['type'=>3,'user_id'=>$member['id'],'status'=>1]);
            if(!$alipay){
                return ajaxreturn(1, lang('您还没绑定银行卡收款方式'));
            }
        }

        $price=$wallet['parities'];
        $data['number']=$data['money']/$price;
        $data['surplus_number']=$data['number'];
        $data['price']=$price;
        $data['total']=$data['money'];
        $data['surplus_total']= $data['total'];

        if(isset($data['paytype'])){
            unset($data['paytype']);
        }
        $money_price=getPrice();
        if(!in_array($data['surplus_total'],$money_price)){
            return ajaxreturn(1,lang('金额错误'));
        }
        $data['user_id']=$member['id'];
        if($data['type']==1){
            $shop_account=$this->modelOtcShop->where(['user_id'=>$member['id'],'state'=>2])->select();
            $total=0;
            foreach ($shop_account as $k=>$v){
                $total+=$v['number_do']*$v->wallet->parities;
                $total+=$v['cancel_number']*$v->wallet->parities;
            }
            $max_money=getConfig('shop_balance_money');
            if($total<$max_money){
                return ajaxreturn(1, lang('您的店铺余额不够'.$max_money.'CNY,不能直接发布提现代发订单，请先充值激活店铺。'));
            }
        }

        Db::startTrans();
        $shop=$this->modelOtcShop->where(['id'=>$data['shop_id']])->lock(true)->find();
        if(!$shop){
            return ajaxreturn(1, lang('商铺不存在'));
        }
        if( $shop['user_id']!=$member['id']){
            return ajaxreturn(1, lang('该商铺不是您的'));
        }
        if($data['type']==1){
            $trade_count=OtcTrading::where(['type'=>1,'shop_id'=>$shop['id'],'state'=>0])->count();
            if($trade_count>=getConfig('otc_max_withdrwal_count')){
                return ajaxreturn(1, lang('为了更加公平的合理排队机制，您目前已经达到代发订单的上限，感谢您的为法币OTC代发的政策策略的支持和理解'));
            }
        }
        if($data['type']==2){
            if( $shop['number_do']+$shop->cancel_number<$data['number'] ){
                return ajaxreturn(1, lang('商铺余额不足'));
            }
        }
        if($shop['is_release']==0){
            if($shop['penalty_time'] && $shop['penalty_time']>TIME_NOW){
                return ajaxreturn(1, lang('您已被限制发布订单,解除时间为').date('Y-m-d H:i:s',$shop['penalty_time']));
            }else{
                return ajaxreturn(1, lang('您不能发布订单'));
            }

        }

        try{
            //生成订单
            $this->modelOtcTrading->setInfo($data);
//        dd($data);
            if($data['type']==2){
                if($data['number']>$shop->cancel_number){
                    $second_number=$data['number']-$shop->cancel_number;
                    $cancel_number=$shop->cancel_number;
                    $shop->cancel_number=0;
                    $shop->number_do-=$second_number;
                    if($cancel_number!=0){
                        ShopAccountLog::create([
                            'user_id'=>$member['id'],
                            'number'=>$cancel_number,
                            'shop_id'=>$shop['id'],
                            'remark'=> '发布出售订单',
                            'is_add'=>2,
                            'type'=>2
                        ]);
                    }
                    ShopAccountLog::create([
                        'user_id'=>$member['id'],
                        'number'=>$second_number,
                        'shop_id'=>$shop['id'],
                        'remark'=> '发布出售订单，成功扣除',
                        'is_add'=>2,
                        'type'=>1
                    ]);
                }else{
                    $shop->cancel_number-=$data['number'];
                    ShopAccountLog::create([
                        'user_id'=>$member['id'],
                        'number'=>$data['number'],
                        'shop_id'=>$shop['id'],
                        'remark'=> '发布出售订单',
                        'is_add'=>2,
                        'type'=>1
                    ]);
                }

                 $shop->number_lock+=$data['number'];
                
                 $shop->save();
            }
            if($data['type']==1){
                user_notice($member['id'],'您的代发订单已发布成功');

            }else{
                user_notice($member['id'],'您的出售订单已发布成功');

            }
            Db::commit();
            return ajaxreturn(0, lang('恭喜您的订单发布成功,正在为您排队.....'));
        }catch (\Exception $e){
            Db::rollback();
            return ajaxreturn(1, $e->getMessage());
            return ajaxreturn(1, '服务器繁忙,请稍后再试');
        }



    }

    /***
     * 获取用户收款方式详情
     */
    public function get_user_pay_info($data){
        $info=$this->modelUserPayType->getInfo(['type'=>$data['type'],'user_id'=>$data['user_id'],'status'=>1]);
        $info['image']=$info['image'];
        return $info;
    }


    /***
     * 查询用户
     */

    public function get_user($data){
         if(!empty($data['mobile'])){
            $where['username']=$data['mobile'];
        }
         $info=Member::field('id,team_number,username,mobile,dai_level')->where($where)->find();
         if(!$info){
             return ajaxreturn(1,lang('用户不存在'));
         }
         if($info['team_number']!=$this->logicApiBase->member['team_number']){
             return ajaxreturn(1,lang('该用户不是您的团队成员'));
         }
         $info['algebra']=$info['dai_level']-$this->logicApiBase->member['team_number'];
         return $info;
    }

    /**
     * 获取支付方式列表
     */
    public function get_paytype_info($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        $info=$this->modelUserPayType->getInfo(['user_id'=>$uid,'id'=>$data['id'],'type'=>['lt',4]]);
        $info['image']=$info->image;
        return $info;

    }

    /**
     * 组织图
     */
    public function organization($data){
        $member=$this->logicApiBase->member;
        $uid=$member['id'];
        $where=['leader_id'=>$uid];
        $where1=[];
        switch($data['type']){
            case  1:
                //今日
                $where['create_time']=['between',[getTodayStart(),getTodayEnd()]];

                break;
            case  2:
                //昨日
                $where['create_time']=['between',[getTodayStart()-86400,getTodayEnd()-86400]];
                break;
            case  3:
                //近七天
                $where['create_time']=['between',[getTodayStart()-86400*7,getTodayEnd()]];
                break;
            case  4:
                //近30天
                $where['create_time']=['between',[getTodayStart()-86400*30,getTodayEnd()]];


                break;
            default :

                $where=[];


                break;


        }
//
        if(!empty($data['search_data'])){

            $where['username']=['like','%'.$data['search_data'].'%'];
        }
        $info=[];
        $info['children_count']=$member['children_count'];
        $info['max_children_count']=get_max_number($member['children_count']);

        $info['san_count']=$member['san_count'];
        $info['max_san_count']=get_max_number($member['san_count']);

        $ids=Member::where(['path'=>['like',','.$uid.',']])->column('id');
        if($ids){
            $info['achievement']=UserKingInvestment::where(['user_id'=>['in',$ids],'state'=>['in',[0,2] ] ])->where($where1)->sum('money')+0;
            $info['team_profit']=UserAccountLog::where(['user_id'=>['in',$ids],'type'=>['in',[10,11,12]]])->where($where1)->sum('number')+0;
        }else{
            $info['team_profit']=0;
            $info['achievement']=0;
        }

        $info['max_achievement']=get_max_number($member['achievement']);

        $children_ids=explode(',',$member['ceng_member']);
        if($children_ids){
            $info['share_achievement']=$member['share_achievement'];
            $info['share_profit']=UserAccountLog::where(['user_id'=>$member['id'],'uid'=>['in',$children_ids],'type'=>['in',[10,11]]])->where($where1)->sum('number')+0;
            $info['share_static_profit']=UserAccountLog::where(['user_id'=>$member['id'],'uid'=>['in',$children_ids],'type'=>9])->where($where1)->sum('number')+0;
        }else{
            $info['share_achievement']=0;
            $info['share_profit']=0;
            $info['share_static_profit']=0;
        }

        $info['max_share_achievement']=get_max_number($member['share_achievement']);



        $info['max_team_profit']=get_max_number($info['team_profit']);



        $info['max_share_profit']=get_max_number($info['share_profit']);


        $info['max_share_static_profit']=get_max_number($info['share_static_profit']);

        $info['game_point']=GamesLog::where(['user_id'=>$member['id'],'type'=>6])->where($where1)->sum('number')+0;
        $info['max_game_point']=get_max_number($info['game_point']);


        if(!empty($data['search_data'])){
            $where['path']=['like','%,'.$uid.',%'];
            $list=$this->modelMember->getList($where,'is_shop,level_id,ceng_member,path,username,id,mobile,share_achievement,achievement,cover_id,level_id','create_time desc',false);
        }else{
            $where['id']=['in',$children_ids];
            $list=$this->modelMember->getList($where,'is_shop,level_id,ceng_member,path,username,id,mobile,share_achievement,achievement,cover_id,level_id','create_time desc',false);
        }

         foreach ($list as $k=>$v){
             $list[$k]['avtar']=$v->avtar;
             $list[$k]['leader_point']=$member['game_point_money'];
             $list[$k]['children_count']=$v->children_count;
             $list[$k]['level_name']=$v->level->level_name;
             $list[$k]['game_point_money']=$v->level->game_return;
             $list[$k]['level_nick_name']=$v->level->nickname;
             $list[$k]['san_count']=$v->san_count;

             $list[$k]['bets_total']=$v->bets_total;
             $list[$k]['profit_total']=$v->user_win+$v->investment_win+$v->user_lose+$v->investment_lose;
             $list[$k]['san_count']=$v->san_count;
             $list[$k]['king_count']=Member::where(['path'=>['like',','.$v['id'].','],'is_king'=>1])->count();
             $list[$k]['shop_count']=Member::where(['path'=>['like',','.$v['id'].','],'is_shop'=>1])->count();
             $ids=Member::where(['path'=>['like','%,'.$v['id'].',%']])->column('id');
             $list[$k]['team_profit']=format_money(UserAccountLog::where(['user_id'=>$v['id'],'uid'=>['in',$ids],'type'=>['in',[10,11,12]]])->sum('number')+0,1);
             $list[$k]['share_static_profit']=format_money(UserAccountLog::where(['user_id'=>$v['id'],'type'=>9])->sum('number')+0,2);
             $list[$k]['game_point']=GamesLog::where(['user_id'=>$v['id'],'type'=>6])->sum('number')+0;




             unset($v->level);
         }
         $info['user_list']=$list;
         return  $info;

    }


    /***
     * 直推用户列表
     */

    public function  share_user($data){
        $uid=$data['id'];
        $where=['leader_id'=>$uid];
        if(!empty($data['search_data'])){

            $where['username']=['like','%'.$data['search_data'].'%'];
        }
        $member=Member::get($data['id']);
        $list=$this->modelMember->getList($where,'is_shop,level_id,ceng_member,path,username,id,mobile,share_achievement,achievement,cover_id,level_id','create_time desc');
        foreach ($list as $k=>$v){
            $list[$k]['avtar']=$v->avtar;
            $list[$k]['show']=0;
            $list[$k]['leader_point']=$member['game_point_money'];
            $list[$k]['point_one']=getConfig('point_one');
            $list[$k]['max_point']=getConfig('set_point');
            $list[$k]['level_name']=$v->level->level_name;
            $list[$k]['level_nick_name']=$v->level->nickname;
            $list[$k]['game_point_money']=$v->level->game_return;
            $list[$k]['children_count']=$v->children_count;
            $list[$k]['san_count']=$v->san_count;
            $list[$k]['bets_total']=$v->bets_total;
            $list[$k]['profit_total']=$v->user_win+$v->investment_win+$v->user_lose+$v->investment_lose;
            $list[$k]['king_count']=Member::where(['path'=>['like',','.$v['id'].','],'is_king'=>1])->count();
            $list[$k]['shop_count']=Member::where(['path'=>['like',','.$v['id'].','],'is_shop'=>1])->count();
            
            $ids=Member::where(['path'=>['like','%,'.$v['id'].',%']])->column('id');
            
        
            if($ids){
                $list[$k]['team_profit']=UserAccountLog::where(['user_id'=>$v['id'],'uid'=>['in',$ids],'type'=>['in',[10,11,12]]])->sum('number')+0;
            }else{
                $list[$k]['team_profit']=0;
            }

            $list[$k]['share_static_profit']=UserAccountLog::where(['user_id'=>$v['id'],'type'=>9])->sum('number')+0;
            $list[$k]['game_point']=GamesLog::where(['user_id'=>$v['id'],'type'=>6])->sum('number')+0; 
            unset($v->level);
        }
        return $list;
    }


    /***
     * 会员详情
     */

    public function user_detail($data)
    {
        $member=Member::field('id,username,achievement,is_shop,is_king,ceng_member,level_id,share_achievement,create_time,leader_id')->where(['id'=>$data['id']])->find();
        $leader=Member::get($member['leader_id']);
        $ids=Member::where(['path'=>['like',','.$member['id'].',']])->column('id');
        $children_ids=explode(',',$member['ceng_member']);
        $member['children_count']=$member->children_count;
        $member['san_count']=$member->san_count;
        $member['reg_time']=date('Y-m-d H:i:s',$member['create_time']);
        $member['leader_point']=$leader['game_point_money'];
        $member['game_point_money']=$member->level->game_return;
        unset($member->level);
        $member['point_one']=getConfig('point_one');
        $member['max_point']=getConfig('set_point');
        if($ids){
            $member['team_bets']=BetsLog::where(['user_id'=>['in',$ids]])->sum('number')+0;
        }else{
            $member['team_bets']=0;
        }
        if($children_ids){
            $member['share_bets']=BetsLog::where(['user_id'=>['in',$children_ids]])->sum('number')+0;
        }else{
            $member['share_bets']=0;
        }

        return $member;





    }


    /**
     * 设置返点
     */
    public function set_point($data){
        $member1=$this->logicApiBase->member;
        $uid=$member1['id'];

        $member=Member::get($data['id']);
        $leader=Member::get($member['leader_id']);
        if($uid!=$member['leader_id']){
            return ajaxreturn(1,lang('该账号不是您得下级'));
        }
        $set_point=getConfig('set_point');
        if($data['game_point_money']<$leader['game_point_money']*(100-$set_point)/100){
            return ajaxreturn(1,lang('返点数错误'));
        }
        $child_point=Member::where(['leader_id'=>$member['id']])->order('game_point_money desc')->value('game_point_money');
        if($child_point && $data['game_point_money']<$child_point ){
            return ajaxreturn(1,lang('返点数不能小于该用户下级返点值'));
        }
        $member->game_point_money=$data['game_point_money'];
        $member->save();
        return ajaxreturn(0,lang('设置成功'));

    }


    /**
     * 精准开户
     */
    public function account_opening($data){

        $member=$this->logicApiBase->member;
        $uid=$member['id'];
        if (empty($data['username'])) {
            return ajaxreturn(1, lang('用户名或密码不能为空'));
        }
        if(preg_match('/[\x{4e00}-\x{9fa5}]+/u',$data['username'])){
            return ajaxreturn(1, lang('用户名不能有中文'));
        }
        if (strlen($data['username'])<3)
        {
            return ajaxreturn(1, lang('用户名至少3位起'));
        }

        if(!preg_match("/^[a-z\d]*$/i",$data['username']))
        {
            return ajaxreturn(1, lang('用户名必须包含数字和字母'));
        }
        if(replaceSpecialChar($data['username'])){
            return ajaxreturn(1, lang('用户名不能有特殊字符或者空格'));
        }
        if (strlen( $data['password']) < 6) {
            return ajaxreturn(1, lang('登录密码不能小于6位'));
        }
        if ( $data['password'] == '123456') {
            return ajaxreturn(1, lang('您的登录密码设置的太过简单，请不要使用123456作为登录密码'));
        }
//        if (strlen($data['pin_password']) != 6) {
//            return ajaxreturn(0, lang('PIN密码不能小于6位'));
//        }
//        if ( $data['pin_password'] == '123456') {
//            return ajaxreturn(1, lang('您设置的交易密码太过简单'));
//        }
//        if (!preg_match('/^[0-9]*[1-9][0-9]*$/' ,$data['pin_password'])) {
//            return ajaxreturn(0, lang('二级密码必须是数字'));
//        }
//
//        if(!$this->check_mobile_code($data)){
//
//            return ajaxreturn(1, lang('验证码错误'));
//
//        }
//        $set_point=getConfig('set_point');
//        if($data['game_point_money']+$set_point<$member['game_point_money']){
//            return ajaxreturn(1,lang('返点数错误'));
//        }
        $data['invite_code']=$this->degui();
        $data['leader_id']=$uid;
//        $pin_password=$data['pin_password'];
//        $data['pin_password']= md5($data['pin_password'].'pin');


        Db::startTrans();
        $user = Member::where(['username'=>$data['username']])->find();
        if (!empty($user)) {
            return ajaxreturn(1, lang('用户名已存在，请更换'));
        }

//        $user = Member::where(['mobile'=>$data['mobile']])->find();
//        if (!empty($user)) {
//            return ajaxreturn(1, lang('手机号已注册，请更换'));
//        }
        try{

            $result = $this->modelMember->setInfo($data);

            //创建账户
            UserAccount::addWallet($result);
            $member1=$this->modelMember->getInfo(['id'=>$result]);


            //写入邀请人层级
            if($member['ceng_member']){
                $member->ceng_member=$result.','.$member['ceng_member'];

            }else{
                $member->ceng_member=$result;
            }
            $member1->path=$result.','. $member->path;
            $member1->dai_level=$member->dai_level+1;
            $member1->team_id=$member['team_id'];
            $member1->team_number=$member['team_number'];

            $member1->save();
            $member->save();

            //消息推送
            $path=rtrim( $member->path,',');
            $path=explode(',',$path);
            $reg_type='APP精准开户';
            $notices=[];
            $ipinfo=get_ip_info(get_client_ip());
            $city=$ipinfo?$ipinfo['city']:'';
            foreach ($path as $k =>$v){
                $notice=[
                    'user_id'=>$v,
                    'create_time'=>TIME_NOW,
                    'type'=>1,
                ];
                if($k==0){
                    $notice['title']='您有新增下级成员'.$data['username'].'，来源于通过'.$reg_type.'方式注册成功' ;
                }else{

                    $notice['title']='您有新团队成员'.$data['username'].'（第'.($k+1).'代）加入，通过'.$reg_type.'方式注册成功。' ;
                }
                $json_arr=[
                    'username'=>$data['username'],
                    'leader_name'=>$member->username,
                    'reg_time'=>date('Y-m-d H:i:s'),
                    'reg_type'=>$reg_type,
                    'reg_city'=>$city
                ];
                $notice['json_msg']=json_encode($json_arr);
                $notices[]=$notice;
            }
            \app\common\model\UserNotice::insertall($notices);
            $game_point_money=UserLevel::get(1);
            $paths=Member::where(['id'=>['in',explode(',',$member->path)],'level_id'=>['gt',1]])->column('id');
            //团队升级
            $member->upgrade_level($paths);
            Db::commit();

            $info= $this->modelVersion->field( 'id,title,content,version,download_url_ios,download_url,create_time')->where(['is_enable'=>1,'status'=>1])->order('version desc')->find();
            return ['username'=>$data['username'],'password'=>$data['password'],'game_point_money'=>$game_point_money['game_return'],'android'=>$info['download_url'],'ios'=>$info['download_url_ios']];
        }catch ( \Exception $e){
            Db::rollback();
            return ajaxreturn(1, $e->getMessage());
            return ajaxreturn(1, lang('服务器繁忙,请稍后再试'));
        }

    }


    /**
     * 用户反馈
     */
    public function feedback($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        if(empty($data['content'])){
            return ajaxreturn(1,lang('请输入反馈内容'));
        }
        if(empty($data['contact'])){
            return ajaxreturn(1,lang('请输入联系方式'));
        }

        $data['user_id']=$uid;
        $this->modelUserFeedback->setInfo($data);
        return ajaxreturn(0,lang('反馈成功'));
    }


    /**
     * 用户消息
     */
    public function user_notice_list($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        UserNotice::where(['user_id'=>$uid,'is_read'=>0])->update(['is_read'=>1]);
        $list=$this->modelUserNotice->getList(['user_id'=>$uid],'id,title,content,create_time','create_time desc');
        return $list;
    }

    /**
     * 用户消息详情
     */
    public function user_notice_info($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        $info=$this->modelUserNotice->getInfo(['id'=>$data['id'],'user_id'=>$uid]);
        $info->is_read=1;
        $info->save();
        return $info;
    }


    /***
     * 下注记录
     */

    public function  bets_log($data){
        if($_SERVER['REMOTE_ADDR']!='47.91.251.193'){
            return ajaxreturn(1, lang('您的IP地址不允许访问'));
        }
        $member=Member::get(['user_id'=>$data['user_id']]);
        if(!$member){
            return ajaxreturn(1, lang('用户不存在'));
        }
//        if(!$member->is_clean_code()){
//            return ajaxreturn(0, lang('下注成功'));
//        }
        Db::startTrans();
        try{
            $clean_code_proportion=getConfig('clean_code_proportion');
            $number=$data['number']*$clean_code_proportion/100;
            BetsLog::create([
                'user_id'=>$member['id'],
                'number'=>$number,
            ]);
            $user_account=UserAccount::check(['user_id'=>$member['id'],'wallet_id'=>5],false);
            $user_account->balance+=$number;
            $user_account->save();
            //写入资金流水
//            UserAccountLog::create([
//                'user_id'=>$user_account['user_id'],
//                'number'=>$number,
//                'wallet_id'=>$user_account['wallet_id'],
//                'before_blance'=>$user_account->balance-$data['number'],
//                'after_blance'=>$user_account->balance,
//                'is_add'=>1,
//                'type'=>18
//            ]);
            Db::commit();
            return ajaxreturn(0, lang('下注成功'));
        }catch (\Exception $e){
            Db::rollback();
            return ajaxreturn(1,$e->getMessage());
        }

        //BetsLog::get(['order_no'])
    }


    /***
     * 游戏转入总包
     */

    public function  transfer_to_wallet($data){
        $arr= ['47.91.251.193'];
        if(  !in_array( $_SERVER['REMOTE_ADDR'],$arr)){
            return ajaxreturn(1, lang('您的IP地址不允许访问'));
        }
        $order=$this->getExchangeOrder($data['orderId']);
        if(!$order){
            return ajaxreturn(1, lang('订单不存在'));
        }
        if(Transfer::where(['remark'=>$data['orderId'],'type'=>15])->count()>0){
            return ajaxreturn(1, lang('订单已兑换'));
        }

        if($order['status']=='SUCCESS' ||  $order['status']=='FAIL'){
            return ajaxreturn(1, lang('订单状态错误'));
        }
//        if(!$member->is_clean_code()){
//            return ajaxreturn(0, lang('下注成功'));
//        }
        $member=Member::get(['user_id'=>$order['userId']]);
        if(!$member){
            return ajaxreturn(1, lang('用户不存在'));
        }
        $this->exchange_pay($data['orderId']);
        $total=$order['amount']-$order['tax'];
        Db::startTrans();
        try{
            Transfer::create([
                'user_id'=>$member['id'],
                'number'=>$order['amount'],
                'poundage_number'=>$order['tax'],
                'type'=>15,
                'money'=>$total,
                'remark'=>$order['id'],
                'state'=>1
            ]);
            $user_account=UserAccount::check(['user_id'=>$member['id'],'wallet_id'=>1],false);
            $user_account->balance+=$total;
            $user_account->save();
            //写入资金流水
            UserAccountLog::create([
                'user_id'=>$user_account['user_id'],
                'number'=>$total,
                'wallet_id'=>$user_account['wallet_id'],
                'before_blance'=>$user_account->balance-$total,
                'after_blance'=>$user_account->balance,
                'remark'=>'游戏金币转入总包',
                'is_add'=>1,
                'type'=>19
            ]);
            Db::commit();
            return ajaxreturn(0, lang('转入成功'));
        }catch (\Exception $e){
            Db::rollback();
            return ajaxreturn(1,$e->getMessage());
        }
    }


    //订单列表

    public function order_list($data){
        $member=$this->logicApiBase->member;
        $uid=$member['id'];
        switch($data['type']){
            case  1:
                //今日
                $where['create_time']=['between',[getTodayStart(),getTodayEnd()]];

                break;
            case  2:
                //昨日
                $where['create_time']=['between',[getTodayStart()-86400,getTodayEnd()-86400]];
                break;
            case  3:
                //近七天
                $where['create_time']=['between',[getTodayStart()-86400*7,getTodayEnd()]];
                break;
            case  4:
                //近30天
                $where['create_time']=['between',[getTodayStart()-86400*30,getTodayEnd()]];


                break;
            default :

                $where=[];


                break;


        }

        if(!empty($data['start_time']) && !empty($data['end_time'])){

            $where['create_time']=['between',[strtotime($data['start_time'].' 00:00:00'),strtotime($data['end_time'].' 23:59:59') ]];
        }

        switch ($data['order_type']){
            //OTC交易
            case 1:
                if(!empty($data['withdrwal_id'])){
                    $where['withdrwal_id']=$data['withdrwal_id'];
                    $where['uid']=$member['id'];
                }else{
                    $where['user_id|uid']=$member['id'];
                }

                $list=$this->modelOtcOrder->getList($where,'id,uid,user_id,serial_number,pay_type,wallet_id,create_time,number,state,total','id desc');
                foreach ($list as $k =>$v){

                    $list[$k]['pay_name']=$v->pay_text;
                    $list[$k]['state_text']=$v->state_text;
                    $wallet_name=$v->wallet->getData('name');
                    $list[$k]['wallet_name']=$wallet_name;

                    if($v['user_id']==$uid){
                        $msg=lang('买入');
                    }else{
                        $msg=lang('出售');
                    }
                    $list[$k]['order_name']=$msg.' '.$wallet_name;
                    if($v['uid']==$uid){
                        $list[$k]['is_add']=2;
                    }else{
                        $list[$k]['is_add']=1;
                    }
                    unset($v->wallet);
                }

                break;

            case 2:
                //区块链上充值
                $where['user_id']=$uid;
                $where['type']=10;
                $list=$this->modelTransfer->getList($where,'id,create_time,serial_number,form_wallet_id,number,state,hash,txFee,from','id desc');
                foreach ($list as $k=>$v){

                    $wallet_name=$v->wallet->getData('name');
                    $list[$k]['order_name']=lang('跨链转入').' '.$wallet_name;
                    $list[$k]['wallet_name']=$v->wallet->data['name'];
                    unset($v->wallet);



                }
                break;

            case 3:
                //提币记录

                $where['user_id']=$uid;
                $where['type']=20;
                $list=$this->modelTransfer->getList($where,'id,create_time,form_wallet_id,serial_number,number,txFee,poundage_number,state,to','id desc');
                foreach ($list as $k=>$v){
                    $wallet_name=$v->wallet->getData('name');
                    $list[$k]['order_name']=lang('申请提币').' '.$wallet_name;
                    $list[$k]['get_number']=format_money($v['number']-$v['poundage_number'],8);
                    unset($v->wallet);



                }

                return $list;
                break;

            case 4:
                //转账
                $where['user_id']=$uid;
                $where['type']=1;
                $list=$this->modelTransfer->getList($where,'id,create_time,form_wallet_id,serial_number,number,serial_number,state,poundage_number,from','id desc');
                foreach ($list as $k=>$v){

                    $wallet_name=$v->wallet->getData('name');
                    $list[$k]['order_name']=lang('转账').' '.$wallet_name;
                    $list[$k]['wallet_name']=$v->wallet->data['name'];
                    unset($v->wallet);



                }
                break;
            case 5:
                //兑换记录
                $where['user_id']=$uid;
                $where['type']=19;
                $list=$this->modelTransfer->getList($where,'id,create_time,form_wallet_id,serial_number,to_wallet_id,cc_number,number,remark,state','id desc');
                foreach ($list as $k=>$v){

                    $wallet_name=$v->wallet->getData('name');
                    $to_wallet_name=$v->towallet->getData('name');
                    $list[$k]['order_name']=$wallet_name.' '.lang('兑换').' '.$to_wallet_name;

                    unset($v->wallet);
                    unset($v->towallet);


                }
                break;
            case 6:
                //划转记录
                $where['user_id']=$uid;
                $where['type']=['in',[16,17]];
                $list=$this->modelTransfer->getList($where,'id,type,create_time,form_wallet_id,serial_number,to_wallet_id,number,remark,state','id desc');
                foreach ($list as $k=>$v){

                    $wallet_name=$v->wallet->getData('name');
                    if($v['type']==16){
                        $list[$k]['order_name']=lang('币币').' '.$wallet_name.' '.lang('划转法币').' '.$wallet_name;
                    }else{
                        $list[$k]['order_name']=lang('法币').' '.$wallet_name.' '.lang('划转币币').' '.$wallet_name;
                    }
                    unset($v->wallet);


                }
                break;

            case 7:
                //法币转总包记录
                $where['user_id']=$uid;
                $where['type']=22;
                $list=$this->modelTransfer->getList($where,'id,create_time,form_wallet_id,serial_number,to_wallet_id,cc_number,number,remark,state','id desc');
                foreach ($list as $k=>$v){

                    $wallet_name=$v->wallet->getData('name');
                    if($v['form_wallet_id']==1){
                        $list[$k]['order_name']=lang('总包').' '.lang('转入法币').' '.$wallet_name;
                    }else{
                        $list[$k]['order_name']=lang('法币').' '.$wallet_name.' '.lang('转入总包');
                    }
                    unset($v->wallet);
                }
                break;
            case 8:
                //法币转总包记录
                $where['user_id']=$uid;
                $where['type']=23;
                $list=$this->modelTransfer->getList($where,'id,create_time,form_wallet_id,serial_number,number,remark,state','id desc');
                foreach ($list as $k=>$v){

                    $wallet_name=$v->wallet->getData('name');
                    $list[$k]['order_name']=$wallet_name.' '.lang('划转店铺');
                    unset($v->wallet);
                }
                break;
            case 9:
                //总包提现记录
                $where['user_id']=$uid;
                $where['type']=3;
                $list=$this->modelTransfer->getList($where,'id,create_time,form_wallet_id,serial_number,number,remark,state','id desc');
                foreach ($list as $k=>$v){
                    switch ($v->withpay->type){
                        case 1:
                            $list[$k]['order_name']=lang('提现支付宝');
                            break;
                        case 2:
                            $list[$k]['order_name']=lang('提现微信');

                            break;
                        case 3:
                            $list[$k]['order_name']=lang('提现银行卡');
                            break;
                    }
                    $list[$k]['order_name']="总包提现";
                    $list[$k]['pay_method']=$v->withpay->getData('type');
                    $list[$k]['account_name']=$v->withpay->bank_card;
                    unset($v->withpay);
                }
                break;


        }

        return $list;
    }


    //订单列表

    public function new_otc_order_list($data){
        $member=$this->logicApiBase->member;
        $uid=$member['id'];
        $where=[];
        if(!empty($data['type']) || isset($data['type'])){
            switch($data['type']){
                case  1:
                    //充值购买
                    $where['user_id']=$member['id'];
                    $where['otc_type']=1;
                    break;
                case  2:
                    //提现商家代发
                    $where['withdrwal_id']=['gt',0];
                    $where['uid']=$member['id'];
                    break;

                default  :
                    $ids=OtcShop::where(['user_id'=>$uid])->column('id');
                    $where['shop_id']=['not in',$ids];
                    $where['user_id|uid']=$member['id'];
                    break;

            }
        }

        if(!empty($data['shop_type'])) {
            switch ($data['shop_type']) {
                case  1:
                    //出售
                    $where['otc_type'] = 2;
                    $where['uid'] = $member['id'];

                    break;
                case  2:
                    //代发
                    $where['otc_type'] = 1;
                    break;

                default  :


                    break;

            }
        }
        if(!empty($data['shop_id'])) {
            $where['shop_id']=$data['shop_id'];
        }
        switch($data['date_type']){
            case  1:
                //今日
                $where['create_time']=['between',[getTodayStart(),getTodayEnd()]];


                break;
            case  2:
                //昨日
                $where['create_time']=['between',[getTodayStart()-86400,getTodayEnd()-86400]];

                break;
            case  3:
                //近七天
                $where['create_time']=['between',[getTodayStart()-86400*7,getTodayEnd()]];

                break;
            case  4:
                //近30天
                $where['create_time']=['between',[getTodayStart()-86400*30,getTodayEnd()]];


                break;


        }
        if(!empty($data['start_time']) && !empty($data['end_time'])){

            $where['create_time']=['between',[strtotime($data['start_time'].' 00:00:00'),strtotime($data['end_time'].' 23:59:59') ]];
        }
        if(!empty($data['state'])) {
            $where['state']=$data['state'];
        }




        $list=$this->modelOtcOrder->getList($where,'id,uid,user_id,serial_number,pay_type,wallet_id,create_time,number,state,total','id desc');
        foreach ($list as $k =>$v){

            $list[$k]['pay_name']=$v->pay_text;
            $list[$k]['state_text']=$v->state_text;
            $wallet_name=$v->wallet->getData('name');
            $list[$k]['wallet_name']=$wallet_name;
            if(!empty($data['shop_id'])){
                if($v['user_id']==$uid){
                    $msg=lang('提现代发');
                }else{
                    $msg=lang('收款代收');
                }
            }else{
                if($v['user_id']==$uid){
                    $msg=lang('充值购买');
                }else{
                    $msg=lang('提现商家代发');
                }
            }

            $list[$k]['order_name']=$msg;
            if($v['uid']==$uid){
                $list[$k]['is_add']=2;
            }else{
                $list[$k]['is_add']=1;
            }
            unset($v->wallet);
        }

        return $list;
    }

    /**
     * 新财务明细接口
     */
    public function new_order_list($data){
        $big_type=$data['big_type'];
        $small_type=$data['small_type'];
        $member=$this->logicApiBase->member;
        $uid=$member['id'];
        $where['user_id']=$uid;
        switch($data['type']){
            case  1:
                //今日
                $where['create_time']=['between',[getTodayStart(),getTodayEnd()]];
                $where1['create_time']=['between',[getTodayStart(),getTodayEnd()]];

                break;
            case  2:
                //昨日
                $where['create_time']=['between',[getTodayStart()-86400,getTodayEnd()-86400]];
                $where1['create_time']=['between',[getTodayStart()-86400,getTodayEnd()-86400]];
                break;
            case  3:
                //近七天
                $where['create_time']=['between',[getTodayStart()-86400*7,getTodayEnd()]];
                $where1['create_time']=['between',[getTodayStart()-86400*7,getTodayEnd()]];
                break;
            case  4:
                //近30天
                $where['create_time']=['between',[getTodayStart()-86400*30,getTodayEnd()]];
                $where1['create_time']=['between',[getTodayStart()-86400*30,getTodayEnd()]];

                break;
            default :

                break;


        }
        if(!empty($data['start_time']) && !empty($data['end_time'])){

            $where['create_time']=['between',[strtotime($data['start_time'].' 00:00:00'),strtotime($data['end_time'].' 23:59:59') ]];
            $where1['create_time']=['between',[strtotime($data['start_time'].' 00:00:00'),strtotime($data['end_time'].' 23:59:59') ]];
        }
        $list=[];
        if($big_type){
            switch ($big_type ) {
                case 1:

                    switch ($small_type) {
                        case 1:
                            $where['type'] = 10;

                            break;
                        case 2:
                            $where['type'] = 12;

                            break;
                        case 3:
                            $where['type'] = 11;
                            break;
                        case 4:
                            $where['type'] = 4;
                            break;
                        default:
                            $where['type'] = ['in', [4,10, 11, 12]];
                            break;
                    }
                    $list = $this->modelUserAccountLog->getList($where, 'type,id,create_time,wallet_id,number,serial_number,remark', 'id desc');
                    foreach ($list as $k => $v) {
                        switch ($v['type']) {
                            case 10:

                                $order_name = lang('分享奖');
                                $list[$k]['small_type'] =1;
                                break;
                            case 12:
                                $list[$k]['small_type'] =2;
                                $order_name = lang('团队奖');
                                break;
                            case 11:
                                $list[$k]['small_type'] =3;
                                $order_name = lang('平级奖');
                                break;
                            case 4:
                                $list[$k]['small_type'] =4;
                                $order_name = lang('贡献奖');
                                break;

                        }
                        $list[$k]['is_add'] =1;
                        $list[$k]['order_name'] = $order_name;
                    }

                    break;
                case 2:
                    switch ($small_type) {
                        case 1:
                            $where['type'] = ['in', [4, 5]];
                            $where['state'] = 3;

                            break;
                        case 2:

                            $where['type'] = 6;
                            break;
                        case 3:

                            $where['type'] = ['in', [4, 5]];
                            $where['state'] = 4;
                            $where['is_winner'] = 1;
                            break;
                        case 4:

                            $where['type'] = ['in', [4, 5]];
                            $where['state'] = 4;
                            $where['is_winner'] = 2;
                            break;
                        default:
                            $where['type'] = ['in', [4, 5, 6]];
                            break;
                    }

                    $field = 'type,remark,profit ,is_winner,number,state,id,create_time,order_no';
                    $list = $this->modelGamesLog->getList($where, $field, 'id desc');
                    foreach ($list as $k => $v) {
                        if ($v['type'] == 6) {
                            $list[$k]['is_add'] =1;
                            $order_name = lang('返点');
                            $list[$k]['small_type'] =2;
                        } else {
                            switch ($v['state']) {
                                case 3:
                                    $list[$k]['is_add'] =2;
                                    $order_name = lang('未开奖');
                                    $list[$k]['small_type'] =1;
                                    break;


                                case 4:
                                    $list[$k]['number'] = $v['profit'];
                                    if ($v['is_winner'] == 1) {
                                        $list[$k]['is_add'] =1;
                                        $order_name = lang('中奖');
                                        $list[$k]['small_type'] =3;
                                    } else {
                                        $list[$k]['is_add'] =2;
                                        $list[$k]['small_type'] =4;
                                        $order_name = lang('未中奖');
                                    }

                                    break;
                            }
                        }

                        $list[$k]['order_name'] = $order_name;
                    }
                    break;
                case 3:
                    switch ($small_type) {
                        case 1:
                            //转账
                            $where['type'] = 1;
                            $list = $this->modelTransfer->getList($where, 'id,remark,create_time,form_wallet_id,serial_number,number,state,poundage_number,from', 'id desc');
                            foreach ($list as $k => $v) {

                                $wallet_name = $v->wallet->getData('name');
                                $list[$k]['order_name'] = lang('转账') . ' ' . $wallet_name;
                                $list[$k]['small_type'] =1;
                                $list[$k]['is_add'] =2;
                                $list[$k]['wallet_name'] = $v->wallet->data['name'];
                                unset($v->wallet);
                            }
                            break;
                        case 2:
                            //区块链上充值
                            $where['type'] = 10;
                            $list = $this->modelTransfer->getList($where, 'id,create_time,remark,serial_number,form_wallet_id,number,state,hash,txFee,from', 'id desc');
                            foreach ($list as $k => $v) {
                                $wallet_name = $v->wallet->getData('name');
                                $list[$k]['order_name'] = lang('跨链转入') . ' ' . $wallet_name;
                                $list[$k]['small_type'] =2;
                                $list[$k]['is_add'] =1;
                                $list[$k]['wallet_name'] = $v->wallet->data['name'];
                                unset($v->wallet);
                            }
                            break;
                        case 3:
                            //划转记录
                            $where['type'] = ['in', [16, 17]];
                            $list = $this->modelTransfer->getList($where, 'id,type,remark,create_time,form_wallet_id,serial_number,to_wallet_id,number,remark,state', 'id desc');
                            foreach ($list as $k => $v) {

                                $wallet_name = $v->wallet->getData('name');
                                if ($v['type'] == 16) {

                                    $list[$k]['order_name'] = lang('币币') . ' ' . $wallet_name . ' ' . lang('划转法币') . ' ' . $wallet_name;
                                } else {
                                    $list[$k]['order_name'] = lang('法币') . ' ' . $wallet_name . ' ' . lang('划转币币') . ' ' . $wallet_name;
                                }
                                $list[$k]['is_add'] =1;
                                $list[$k]['small_type'] =3;
                                unset($v->wallet);


                            }
                            break;
                        case 4:
                            $where['type'] = 3;
                            $list = $this->modelTransfer->getList($where, 'id,remark,create_time,form_wallet_id,serial_number,number,remark,state', 'id desc');

                            foreach ($list as $k => $v) {
                                $list[$k]['is_add'] =2;
                                $list[$k]['small_type'] =4;
                                $list[$k]['order_name'] = "总包提现";
                            }
                            break;
                        case 5:
                            $where['wallet_id'] = 1;
                            $list = $this->modelUserAccountLog->getList($where, 'id,remark,is_add,create_time,number,wallet_id,serial_number,remark', 'id desc');
                            foreach ($list as $k => $v) {
                                $list[$k]['small_type'] =5;
                                $list[$k]['order_name'] = $v['remark'];
                                $list[$k]['state'] = 1;
                            }
                            break;
                        case 6:
                            $where['wallet_id'] = 3;
                            $list = $this->modelUserAccountLog->getList($where, 'id,remark,is_add,create_time,number,wallet_id,serial_number,remark', 'id desc');
                            foreach ($list as $k => $v) {
                                $list[$k]['small_type'] =6;
                                $list[$k]['order_name'] = $v['remark'];
                                $list[$k]['state'] = 1;
                            }
                            break;
                        default:
                            $where['wallet_id'] = ['in', [1, 3]];
                            $list = $this->modelUserAccountLog->getList($where, 'id,remark,is_add,create_time,number,wallet_id,serial_number,remark', 'id desc', false);
                            foreach ($list as $k => $v) {
                                $list[$k]['order_name'] = $v['remark'];
                                if($v['wallet_id']==1){
                                    $list[$k]['small_type'] =5;
                                }else{
                                    $list[$k]['small_type'] =6;
                                }


                            }
                            $where1['user_id'] =$uid;
                            $where1['type'] = ['in', [1, 3, 10, 16, 17]];
                            $list1 = $this->modelTransfer->getList($where1, '*', 'id desc', false);
                            foreach ($list1 as $k => $v) {
                                switch ($v['type']) {
                                    case 1:
                                        $wallet_name = $v->wallet->getData('name');
                                        $list1[$k]['order_name'] = lang('转账') . ' ' . $wallet_name;
                                        $list1[$k]['wallet_name'] = $v->wallet->data['name'];
                                        $list[$k]['is_add'] =2;
                                        $list1[$k]['small_type'] =1;
                                        unset($v->wallet);
                                        break;
                                    case  3:
                                        $list1[$k]['order_name'] = "总包提现";
                                        $list[$k]['is_add'] =2;
                                        $list1[$k]['small_type'] =4;
                                        break;
                                    case  10:
                                        $wallet_name = $v->wallet->getData('name');
                                        $list1[$k]['small_type'] =2;
                                        $list1[$k]['order_name'] = lang('跨链转入') . ' ' . $wallet_name;
                                        $list[$k]['is_add'] =1;
                                        $list1[$k]['wallet_name'] = $v->wallet->data['name'];
                                        unset($v->wallet);
                                        break;
                                    case  16:
                                        $list1[$k]['small_type'] =3;
                                        $wallet_name = $v->wallet->getData('name');
                                        $list[$k]['is_add'] =1;
                                        $list1[$k]['order_name'] = lang('币币') . ' ' . $wallet_name . ' ' . lang('划转法币') . ' ' . $wallet_name;

                                        unset($v->wallet);
                                        break;
                                    case  17:
                                        $list[$k]['small_type'] =3;
                                        $wallet_name = $v->wallet->getData('name');
                                        $list[$k]['is_add'] =1;
                                        $list1[$k]['order_name'] = lang('法币') . ' ' . $wallet_name . ' ' . lang('划转币币') . ' ' . $wallet_name;
                                        unset($v->wallet);
                                        break;

                                }

                            }
                            $newlist = array_merge($list, $list1);
                            $page=empty($data['page'])?1:$data['page'];
                            $pagesize= empty($data['list_rows'])?10:$data['list_rows'];
                            $count = count($list);//总条数
                            $start=($page-1)*$pagesize;//偏移量，当前页-1乘以每页显示条数
                            $newlist = array_slice($newlist,$start,$pagesize);
                            $list = ['total' => $count, 'per_page' => $pagesize, 'current_page' => $page, 'last_page' => 0, 'data' => $newlist];

                            break;
                    }

                    break;
                case 4:

                        switch ($small_type) {
                        case 3:

                                if (!empty($data['withdrwal_id'])) {
                                    $where1['withdrwal_id'] = $data['withdrwal_id'];
                                } else {
                                    $shop_ids=OtcShop::where(['user_id'=>$member['id']])->column('id');
                                    $where1['user_id|uid'] = $member['id'];
                                    $where1['shop_id'] = ['not in',$shop_ids];
                                    $where1['state'] = ['in', [1,8,9]];
                                }

                                $list = $this->modelOtcOrder->getList($where1, 'id,uid,remark,user_id,serial_number,pay_type,wallet_id,create_time,number,state,total', 'id desc');
                                foreach ($list as $k => $v) {
                                    $list[$k]['small_type'] =3;
                                    $list[$k]['pay_name'] = $v->pay_text;

                                    $list[$k]['state_text'] = $v->state_text;
                                    $wallet_name = $v->wallet->getData('name');
                                    $list[$k]['wallet_name'] = $wallet_name;
                                    if ($v['user_id'] == $uid) {
                                        $list[$k]['is_add'] =1;
                                        $msg = lang('买入');
                                    } else {
                                        $list[$k]['is_add'] =2;
                                        $msg = lang('出售');
                                    }
                                    $list[$k]['order_name'] = $msg . $wallet_name;
                                    unset($v->wallet);
                                }

                                break;
                        }


                    break;
                case 5:
                    switch ($small_type) {
                        case 1:
                            $where['number'] = ['gt', 0];
                            $where['type'] = 2;
                            $list = $this->modelBonusLog->getList($where, 'id,remark,create_time,number,serial_number,pei_money', 'id desc');
                            foreach ($list as $k => $v) {
                                $list[$k]['is_add'] =1;
                                $list[$k]['order_name'] = lang('赌王分红');
                                $list[$k]['small_type'] =1;
                            }
                            break;
                        case 2:

                            $list = $this->modelUserKingInvestment->getList($where, 'id,remark,create_time,state,trade_no,money', 'id desc');
                            foreach ($list as $k => $v) {
                                $list[$k]['is_add'] =2;
                                $list[$k]['order_name'] = lang('赌王委托');
                                $list[$k]['small_type'] =2;
                            }
                            break;
                        default:
                            $where['number'] = ['gt', 0];
                            $where['type'] = 2;
                            $list1 = $this->modelBonusLog->getList($where, 'id,remark,create_time,number,serial_number,pei_money', 'id desc', false);
                            foreach ($list1 as $k => $v) {
                                $list1[$k]['small_type'] =1;
                                $list1[$k]['is_add'] =1;
                                $list1[$k]['order_name'] = lang('赌王分红');

                            }
                            $where1['user_id'] = $uid;
                            $list2 = $this->modelUserKingInvestment->getList($where1, 'id,remark,create_time,state,trade_no,money', 'id desc', false);
                            foreach ($list2 as $k => $v) {
                                $list2[$k]['small_type'] =2;
                                $list2[$k]['is_add'] =2;
                                $list2[$k]['order_name'] = lang('赌王委托');

                            }
                            $newlist = array_merge($list1, $list2);
                            $page=empty($data['page'])?1:$data['page'];
                            $pagesize= empty($data['list_rows'])?10:$data['list_rows'];
                            $count = count($list);//总条数
                            $start=($page-1)*$pagesize;//偏移量，当前页-1乘以每页显示条数
                            $newlist = array_slice($newlist,$start,$pagesize);
                            $list = ['total' => $count, 'per_page' => $pagesize, 'current_page' => $page, 'last_page' => 0, 'data' => $newlist];

                            break;
                    }
                    break;
                case 6:
                    switch ($small_type) {
                        case 1:
                            $where1['type'] = 28;
                            $list = $this->modelUserAccountLog->getList($where1, 'type,id,create_time,wallet_id,number,serial_number,remark', 'id desc');
                            foreach ($list as $k => $v) {
                                        $list[$k]['small_type'] =1;
                                        $order_name = lang('OTC交易违反');
                                        
                                $list[$k]['is_add'] =2;
                                $list[$k]['order_name'] = $order_name;
                                
                            }
                            // $where1['user_id|uid'] = $uid;
                            // $where1['state'] = ['in', [6, 7, 8,11]];
                            // $list = $this->modelOtcOrder->getList($where1, 'id,uid,remark,user_id,serial_number,pay_type,wallet_id,create_time,number,state,total', 'id desc');
                            // foreach ($list as $k => $v) {
                            //     $list[$k]['small_type'] =1;
                            //     $list[$k]['pay_name'] = $v->pay_text;
                            //     $list[$k]['state_text'] = $v->state_text;
                            //     $list[$k]['is_add'] =1;
                            //     $wallet_name = $v->wallet->getData('name');
                            //     $list[$k]['wallet_name'] = $wallet_name;

                            //     $list[$k]['order_name'] = lang('申诉中');
                            //     if ($v['uid'] == $uid) {
                            //         $list[$k]['is_add'] = 2;
                            //     } else {
                            //         $list[$k]['is_add'] = 1;
                            //     }
                            //     unset($v->wallet);
                            // }

                            break;
                        case 2:
                            $list = ['total' => 0, 'per_page' => 15, 'current_page' => 1, 'last_page' => 0, 'data' => []];
                            break;
                        case 3:
                            $list = ['total' => 0, 'per_page' => 15, 'current_page' => 1, 'last_page' => 0, 'data' => []];
                            break;
                        default:
                        $where1['type'] = 28;
                        // $list = $this->modelUserAccountLog->getList($where1, 'type,id,create_time,wallet_id,number,serial_number,remark', 'id desc');
                        $list = Db::name("user_account_log")->field("type,id,create_time,wallet_id,number,serial_number,remark")->where($where1)->order("id desc")->select();
                        foreach ($list as $k => $v) {
                                    $list[$k]['small_type'] =1;
                                    $order_name = lang('OTC交易违反');
                                    
                            $list[$k]['is_add'] =2;
                            $list[$k]['order_name'] = $order_name;
                            $list[$k]['create_time'] = date("Y-m-d H:i:s",$v["create_time"]);
                            
                        }
                        $newlist = $list;

                            $page=empty($data['page'])?1:$data['page'];
                            $pagesize= empty($data['list_rows'])?10:$data['list_rows'];
                            $count = count($list);//总条数
                            $start=($page-1)*$pagesize;//偏移量，当前页-1乘以每页显示条数
                            $newlist = array_slice($newlist,$start,$pagesize);
                            $list = ['total' => $count, 'per_page' => $pagesize, 'current_page' => $page, 'last_page' => 0, 'data' => $newlist];



                            break;
                    }

                    break;

                case 7:
                    $shop_ids=OtcShop::where(['user_id'=>$member['id']])->column('id');
                    if(!$shop_ids){
                        $list = ['total' => 0, 'per_page' => 15, 'current_page' => 1, 'last_page' => 0, 'data' => []];
                    }else{
                        if(!empty($where1)){
                            unset($where1['user_id']);
                        }
                        $where1['shop_id']=['in',$shop_ids];
                        !empty($data['small_type']) && $where1['type']=$data['small_type'];
                        $list=$this->modelShopAccountLog->getList($where1,'*','id desc');
                        foreach ($list as $k=>$v){
                            switch ($v['type']){
                                case 1:
                                    $list[$k]['order_name'] = $v->shop->wallet->data['name'].'-'.lang('余额');
                                    break;
                                case 2:
                                    $list[$k]['order_name'] = $v->shop->wallet->data['name'].'-'.lang('冻结');
                                    break;
                                case 3:
                                    $list[$k]['order_name'] = $v->shop->wallet->data['name'].'-'.lang('手续费');
                                    break;
                                case 4:
                                    $list[$k]['order_name'] = $v->shop->wallet->data['name'].'-'.lang('代发');
                                    break;
                                case 5:
                                    $list[$k]['order_name'] = $v->shop->wallet->data['name'].'-'.lang('代收');
                                    break;
                            }
                            $list[$k]['small_type'] =$v['type'];
                        }
                    }

                    break;

            }

        }


        return $list;
    }


    /***
     * 弹窗通知
     */

    public function  user_popup($data){
        $member=$this->logicApiBase->member;
        $uid=$member['id'];
        $where=['user_id'=>$uid,'is_read'=>0];

        if(!empty($data['type'])){
            $type=explode(',',$data['type']);
            $this->modelSbNotice->where(['type'=>['in',$type],'user_id'=>$uid,'is_read'=>0])->update(['is_read'=>1]);
        }
        $list=$this->modelSbNotice->getList($where,'*','id asc',false);

            foreach($list as $k =>$v){
                if($v['type']<4){
                    $list[$k]['wallet_name']=$v->wallet->data['name'];
                    unset($v->wallet);
                    if($v['type']==3){
                        $list[$k]['expire_time1']=$v['expire_time']-TIME_NOW;
                    }
                }


            }

        return $list;


    }


    public function read_notice($data){
        $member=$this->logicApiBase->member;
        $uid=$member['id'];
        $info=$this->modelSbNotice->getInfo(['id'=>$data['id'],'user_id'=>$uid]);
        if(!$info){
            return ajaxreturn(1, lang('消息不存在'));
        }
        $info->is_read=1;
        $info->save();
        return ajaxreturn(0, lang('操作成功'));
    }

    /***
     * 购买会员卡
     */
    public function  buy_member_card(){
        $member=$this->logicApiBase->member;
        $uid=$member['id'];
        Db::startTrans();
        try{
            if($member->level_id!=1){
                return ajaxreturn(1, lang('您当前等级不是玩家'));
            }
            $account=UserAccount::check(['user_id'=>$uid,'wallet_id'=>1],false);
            $money=199;
            if($account['balance']<$money){
                return ajaxreturn(1, lang('总包余额不足'));
            }


            $account->balance -= $money;
            $account->save();
            $member->level_id=2;
            $member->save();
            //写入流水
            UserAccountLog::create([
                'user_id' => $account->user_id,
                'number' => $money,
                'wallet_id' =>$account['wallet_id'],
                'before_blance' => $account->balance + $money,
                'after_blance' => $account->balance,
                'is_add' => 2,
                'type' => 14,
                'remark' =>'购买会员卡',
                
            ]);
            //写入升级记录
            UserLevelLog::create([
                'type'=>1,
                'user_id'=>$member['id'],
                'level'=>2,
                'remark'=>'购买会员卡'
            ]);

            //分享奖


            //上级获得100奖励
            $leader=Member::get($member->leader_id);


            //会员以上
            $leader_account=UserAccount::check(['user_id'=>$leader['id'],'wallet_id'=>1],false);
            $algebra_money =100;
            $leader_account->balance += $algebra_money;
            $leader_account->save();
            //写入代数奖流水
            UserAccountLog::create([
                'user_id' => $leader_account->user_id,
                'number' => $algebra_money,
                'wallet_id' => $leader_account['wallet_id'],
                'before_blance' => $leader_account->balance - $algebra_money,
                'after_blance' => $leader_account->balance,
                'is_add' => 1,
                'type' => 10,
                'level_id' =>  $leader->level_id,
                'is_reward'=>1,
                'uid'=>$member['id'],
                // 'uid'=>$leader['id'],
                'algebra'=>1,
                'remark' =>$member['username'].'给我返红包分享奖',
            ]);
            Db::name("member")->where(["id"=>$leader_account->user_id])->setInc("bets_total_balance",$algebra_money);
            user_notice($leader_account->user_id,'红包分享奖励由新增下级'.$member['username'].'产生的');
            if($member->leader_id>1){
                $member->upgrade_level([$member->leader_id,$member->id]);
            }
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return ajaxreturn(1, $e->getMessage());
        }



    }


    /***
     * 发起应用余额转账
     */
    public function  app_balance_transfer($data){
        $member=$this->logicApiBase->member;
        if(empty($data['number'])){
            return ajaxreturn(1,lang('请输入金额'));
        }
        if(!is_numeric($data['number']) ){
            return ajaxreturn(1,lang('格式不正确'));
        }
        if($data['number']<0){
            return ajaxreturn(1,lang('金额不能为负数'));
        }
        if($member['app_balance']<$data['number']){
            return ajaxreturn(1,lang('余额不足'));
        }
        //        //验证支付密码
        $result=$this->check_password($data['pin_password']);
        switch ($result)
        {
            case 1:
                return  ajaxreturn(1,lang('您的账户操作多次密码验证不正确，如果不是您本人操作，请前往修改密码'));
                break;
            case 2:
                return  ajaxreturn(1,lang('您的密码验证错误2次，您的账户为了账户安全，已被冻结，请在30分钟后再次尝试。'));

                break;
            case 3:
                return  ajaxreturn(1,lang('您的密码验证错误1次，您还可以尝试1次！'));
                break;


        }
        if(!empty($data['email_type'])){
            $user_sms_bean = $this->modelUserEmailLog->where(array('token' => $data['token'], 'email' => $this->logicApiBase->member['user_email'],'code'=>$data['code'], 'status' => 0))->find();
            if (empty($user_sms_bean)) {
                return ajaxreturn(1, lang('验证码错误'));

            }
        }else{
            $user_sms_bean = $this->modelUserSms->where(array('token' => $data['token'],'area'=>$this->logicApiBase->member['area'],'sms_type'=>16, 'mobile' => $this->logicApiBase->member['mobile'],'code'=>$data['code'], 'status' => 0))->find();
            if (empty($user_sms_bean)) {
                return ajaxreturn(1, lang('验证码错误'));

            }
        }

        $data['user_id']=$member['id'];
        $data['money']=$data['number'];
        Db::startTrans();
        try{
            if(getConfig('app_balance_transfer_pattern')==1){
                //模式1不需要后台审核
                $url=getConfig('alipay_url');
                $key=getConfig('alipay_aplkey');
                $arr=[
                    'apikey'=>$key,
                    'payee_account'=>$data['alipay_name'],
                    'amount'=>$data['money'],
                    'payer_show_name'=>getConfig('web_base_name')
                ];

                $res=https_curl($url,$arr,1);
                $res=json_decode($res,true);
                if($res['code']==200){

                    if($res['data'][0]['code']==200){
                        $data['trade_no']=$res['data'][0]['out_biz_no'];
                        $data['state']=1;
                    }else{
                        return ajaxreturn(2,$res['data'][0]['error']);
                    }
                }else{
                    return ajaxreturn(2,$res['msg']);
                }

                $this->modelAppBalanceWithdrawal->setInfo($data);





                //扣除应用余额
                $member->app_balance-=$data['number'];
                $member->save();
                $user_account = [
                    'user_id' => $member['id'],
                    'type' => 5,
                    'king_id' => 0,
                    'is_add' => 2,
                    'remark'=>'线下支付宝扫码支付消费',
                    'create_time' => TIME_NOW,
                    'number' => $data['number']
                ];
                AppAccountLog::create($user_account);
                BonusLog::create([
                    'user_id' => $member['id'],
                    'type' => 4,
                    'number'=>-$data['number'],
                    'remark'=>'线下支付宝扫码支付消费'
                ]);
                Db::commit();
                return ajaxreturn(0,lang('转账成功'));
            }else{
                //模式2需要后台审核
                $data['state']=0;
                $data['data']=0;
                $this->modelAppBalanceWithdrawal->setInfo($data);
                //扣除应用余额
                $member->app_balance-=$data['number'];
                $member->save();
                $user_account = [
                    'user_id' => $member['id'],
                    'type' => 5,
                    'king_id' => 0,
                    'is_add' => 2,
                    'remark'=>'转账提现冻结',
                    'create_time' => TIME_NOW,
                    'number' => $data['number']
                ];
                AppAccountLog::create($user_account);
                Db::commit();
                return ajaxreturn(0,lang('提交成功，请等待管理员审核'));
            }

        }catch (\Exception $e){
            Db::rollback();
            return ajaxreturn(1,$e->getMessage());
        }

    }


    /***
     * 应用余额转账记录
     */
    public function  app_balance_transfer_log($data){
        $member=$this->logicApiBase->member;
        $uid=$member['id'];
        $where=['user_id'=>$uid];
        !empty($data['state']) && $where['state']=$data['state'];

        $list=$this->modelAppBalanceWithdrawal->getList($where,'*','id asc',false);


        return $list;


    }






}
